package mumethod;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import muobject.*;

import org.bouncycastle.util.encoders.Base64;

public class LibFunction implements Base64String, Base64Object, DateControl {

	public String Base64StringEncode(String str) {
		try {
			return new String(Base64.encode(str.getBytes()));
		} catch (Exception e) {
			return str;
		}
	}

	public String Base64StringDecode(String str) {
		try {
			str = str == null ? " " : str;
			return new String(Base64.decode(str.getBytes()));
		} catch (Exception e) {
			return str;
		}
	}

	public School Base64SchoolEncode(School school) {
		school.setCname(Base64StringEncode(school.getCname()));
		school.setEname(Base64StringEncode(school.getEname()));
		return school;
	}

	public School Base64SchoolDecode(School school) {
		school.setCname(Base64StringDecode(school.getCname()));
		school.setEname(Base64StringDecode(school.getEname()));
		return school;
	}

	public Student Base64StudentEncode(Student student) {
		student.setGrade(Base64StringEncode(student.getGrade()));
		student.setSclass(Base64StringEncode(student.getSclass()));
		student.setCname(Base64StringEncode(student.getCname()));
		student.setSex(Base64StringEncode(student.getSex()));
		student.setPassword(Base64StringEncode(student.getPassword()));
		return student;
	}

	public Student Base64StudentDecode(Student student) {
		student.setGrade(Base64StringDecode(student.getGrade()));
		student.setSclass(Base64StringDecode(student.getSclass()));
		student.setCname(Base64StringDecode(student.getCname()));
		student.setSex(Base64StringDecode(student.getSex()));
		student.setPassword(Base64StringDecode(student.getPassword()));
		return student;
	}

	public String getCurrentDateString() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	public long getCurrentDateLong() {
		Date date = new Date();
		return date.getTime();
	}
	
	public String getDataYear(Date date) {
		String currentYear;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if(calendar.get(Calendar.MONTH)<8){
			currentYear = String.valueOf(calendar.get(Calendar.YEAR)-1);
		}else{
			currentYear = String.valueOf(calendar.get(Calendar.YEAR));
		}
		return currentYear;
	}
	
	public int getYearByMonth(int year, int month){
		if(month < 9){
			return year + 1;
		}else{
			return year ;
		}
	}
	
	public int getCurrentMonth() {
		DateFormat dateFormat = new SimpleDateFormat("MM");
		Date date = new Date();
		return Integer.valueOf(dateFormat.format(date));
	}
	
	public String convertDate(String str){
		Date date = new Date();
		DateFormat dateFormat;
		try {
			dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			date = dateFormat.parse(str);
		} catch (Exception e) {
			try {
				dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				date = dateFormat.parse(str);
			} catch (Exception ex) {
				return "";
			}
		}
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}
	
	public String getDateString(Date date){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}
	
	public Date getStringDate(String datestr){
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return dateFormat.parse(datestr);
		} catch (ParseException e) {
			return new Date();
		}
	}
	
	public String getDateType(Date date, String type){
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(date); 
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH)+1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		if(type.equals("year")){
			return String.valueOf(year);
		}else if(type.equals("month")){
			return String.valueOf(month);
		}else if(type.equals("day")){
			return String.valueOf(day);
		}else{
			return "";
		}
	}
	
	public int getStudentAge(Date bornDate, Date pointDate){
		Calendar calendarb = Calendar.getInstance();
		Calendar calendarp = Calendar.getInstance();
		calendarb.setTime(bornDate);
		calendarp.setTime(pointDate);
		if(calendarb.get(Calendar.DAY_OF_YEAR) <= calendarp.get(Calendar.DAY_OF_YEAR)){
			return calendarp.get(Calendar.YEAR) - calendarb.get(Calendar.YEAR);
		}else{
			return calendarp.get(Calendar.YEAR) - calendarb.get(Calendar.YEAR) -1;
		}
	}
	
	public double getBMI(double height, double weight){
		if(height != 0 & weight!= 0)
			return weight/(height*height/10000);
		else
			return 0;
	}
	
	public String EmptyString(String str){
		try {
			return str == null ? "" : str;
		} catch (Exception e) {
			return "";
		}
	}
	
	public double doublePoint(double d){
		java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");  
		return Double.valueOf(df.format(d));
	}
	
	public String StandardBMI(String sex, int age, double BMI){
		
		if(sex.equals("M")){
			if(age==6){
				if(BMI>=19.7){
					return "肥胖";
				}else if(BMI>=17.9&BMI<19.7){
					return "過重";
				}else if(BMI>=14.0&BMI<17.9){
					return "標準";
				}else if(BMI>=0&BMI<14.0){
					return "偏瘦";
				}
			}else if(age==7){
				if(BMI>=21.2){
					return "肥胖";
				}else if(BMI>=18.6&BMI<21.2){
					return "過重";
				}else if(BMI>=14.8&BMI<18.6){
					return "標準";
				}else if(BMI>=0&BMI<14.8){
					return "偏瘦";
				}
			}else if(age==8){
				if(BMI>=22.0){
					return "肥胖";
				}else if(BMI>=19.3&BMI<22.0){
					return "過重";
				}else if(BMI>=15.1&BMI<19.3){
					return "標準";
				}else if(BMI>=0&BMI<15.1){
					return "偏瘦";
				}
			}else if(age==9){
				if(BMI>=22.5){
					return "肥胖";
				}else if(BMI>=19.7&BMI<22.5){
					return "過重";
				}else if(BMI>=15.3&BMI<19.7){
					return "標準";
				}else if(BMI>=0&BMI<15.3){
					return "偏瘦";
				}
			}else if(age==10){
				if(BMI>=22.9){
					return "肥胖";
				}else if(BMI>=20.3&BMI<22.9){
					return "過重";
				}else if(BMI>=15.5&BMI<20.3){
					return "標準";
				}else if(BMI>=0&BMI<15.5){
					return "偏瘦";
				}
			}else if(age==11){
				if(BMI>=23.5){
					return "肥胖";
				}else if(BMI>=21.0&BMI<23.5){
					return "過重";
				}else if(BMI>=15.9&BMI<21.0){
					return "標準";
				}else if(BMI>=0&BMI<15.9){
					return "偏瘦";
				}
			}else if(age==12){
				if(BMI>=24.2){
					return "肥胖";
				}else if(BMI>=21.5&BMI<24.2){
					return "過重";
				}else if(BMI>=16.5&BMI<21.5){
					return "標準";
				}else if(BMI>=0&BMI<16.5){
					return "偏瘦";
				}
			}else if(age==13){
				if(BMI>=24.8){
					return "肥胖";
				}else if(BMI>=22.2&BMI<24.8){
					return "過重";
				}else if(BMI>=17.1&BMI<22.2){
					return "標準";
				}else if(BMI>=0&BMI<17.1){
					return "偏瘦";
				}
			}else if(age==14){
				if(BMI>=25.2){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.2){
					return "過重";
				}else if(BMI>=17.7&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<17.7){
					return "偏瘦";
				}
			}else if(age==15){
				if(BMI>=25.5){
					return "肥胖";
				}else if(BMI>=23.1&BMI<25.5){
					return "過重";
				}else if(BMI>=18.3&BMI<23.1){
					return "標準";
				}else if(BMI>=0&BMI<18.3){
					return "偏瘦";
				}
			}else if(age==16){
				if(BMI>=25.6){
					return "肥胖";
				}else if(BMI>=23.4&BMI<25.6){
					return "過重";
				}else if(BMI>=18.7&BMI<23.4){
					return "標準";
				}else if(BMI>=0&BMI<18.7){
					return "偏瘦";
				}
			}else if(age==17){
				if(BMI>=25.6){
					return "肥胖";
				}else if(BMI>=23.6&BMI<25.6){
					return "過重";
				}else if(BMI>=19.1&BMI<23.6){
					return "標準";
				}else if(BMI>=0&BMI<19.1){
					return "偏瘦";
				}
			}else if(age==18){
				if(BMI>=25.6){
					return "肥胖";
				}else if(BMI>=23.7&BMI<25.6){
					return "過重";
				}else if(BMI>=19.3&BMI<23.7){
					return "標準";
				}else if(BMI>=0&BMI<19.3){
					return "偏瘦";
				}
			}else{
				if(BMI>=25.6){
					return "肥胖";
				}else if(BMI>=23.7&BMI<25.6){
					return "過重";
				}else if(BMI>=19.3&BMI<23.7){
					return "標準";
				}else if(BMI>=0&BMI<19.3){
					return "偏瘦";
				}
			}
		}else if(sex.equals("F")){
			if(age==6){
				if(BMI>=19.1){
					return "肥胖";
				}else if(BMI>=17.2&BMI<19.1){
					return "過重";
				}else if(BMI>=13.7&BMI<17.2){
					return "標準";
				}else if(BMI>=0&BMI<13.7){
					return "偏瘦";
				}
			}else if(age==7){
				if(BMI>=20.3){
					return "肥胖";
				}else if(BMI>=18.0&BMI<20.3){
					return "過重";
				}else if(BMI>=14.5&BMI<18.0){
					return "標準";
				}else if(BMI>=0&BMI<14.5){
					return "偏瘦";
				}
			}else if(age==8){
				if(BMI>=21.0){
					return "肥胖";
				}else if(BMI>=18.8&BMI<21.0){
					return "過重";
				}else if(BMI>=14.7&BMI<18.8){
					return "標準";
				}else if(BMI>=0&BMI<14.7){
					return "偏瘦";
				}
			}else if(age==9){
				if(BMI>=21.6){
					return "肥胖";
				}else if(BMI>=19.3&BMI<21.6){
					return "過重";
				}else if(BMI>=14.9&BMI<19.3){
					return "標準";
				}else if(BMI>=0&BMI<14.9){
					return "偏瘦";
				}
			}else if(age==10){
				if(BMI>=22.3){
					return "肥胖";
				}else if(BMI>=20.1&BMI<22.3){
					return "過重";
				}else if(BMI>=15.3&BMI<20.1){
					return "標準";
				}else if(BMI>=0&BMI<15.3){
					return "偏瘦";
				}
			}else if(age==11){
				if(BMI>=22.3){
					return "肥胖";
				}else if(BMI>=20.1&BMI<22.3){
					return "過重";
				}else if(BMI>=15.3&BMI<20.1){
					return "標準";
				}else if(BMI>=0&BMI<15.3){
					return "偏瘦";
				}
			}else if(age==12){
				if(BMI>=23.9){
					return "肥胖";
				}else if(BMI>=21.6&BMI<23.9){
					return "過重";
				}else if(BMI>=16.5&BMI<21.6){
					return "標準";
				}else if(BMI>=0&BMI<16.5){
					return "偏瘦";
				}
			}else if(age==13){
				if(BMI>=24.6){
					return "肥胖";
				}else if(BMI>=22.2&BMI<24.6){
					return "過重";
				}else if(BMI>=17.1&BMI<22.2){
					return "標準";
				}else if(BMI>=0&BMI<17.1){
					return "偏瘦";
				}
			}else if(age==14){
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=17.7&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<17.7){
					return "偏瘦";
				}
			}else if(age==15){
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=18.1&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<17.7){
					return "偏瘦";
				}
			}else if(age==16){
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=18.3&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<18.3){
					return "偏瘦";
				}
			}else if(age==17){
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=18.4&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<18.4){
					return "偏瘦";
				}
			}else if(age==18){
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=18.4&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<18.4){
					return "偏瘦";
				}
			}else{
				if(BMI>=25.3){
					return "肥胖";
				}else if(BMI>=22.7&BMI<25.3){
					return "過重";
				}else if(BMI>=18.4&BMI<22.7){
					return "標準";
				}else if(BMI>=0&BMI<18.4){
					return "偏瘦";
				}
			}
		}
		return "-";
	}

	public int getAdvice(int score, int total){
		double adviceNo = ((double)score/total)*5;
		if(adviceNo < 1){
			return 1;
		}else if((adviceNo < 2)){
			return 2;
		}else if((adviceNo < 3)){
			return 3;
		}else if((adviceNo < 4)){
			return 4;
		}else if((adviceNo < 5)){
			return 5;
		}
		return 1;
	}
	
	
	public String getAward(int score){
		if(score>=90){
			return "健兒";
		}if(score>=75 & score<90){
			return "活力";
		}if(score>=50 & score<75){
			return "健康";
		}else
			return "緞練";
	}
	
	public String ItemNameConvert(String name){
		if(name.equals("gripscore")){
			return "手握力";
		}else if(name.equals("jumpscore")){
			return "立定跳遠";
		}else if(name.equals("situpscore")){
			return "坐位體前屈";
		}else if(name.equals("vcapacityscore")){
			return "肺活量";
		}else if(name.equals("run69score")){	
			return "6/9分鐘跑";
		}else if(name.equals("run15score")){
			return "15米來回跑";
		}else if(name.equals("totalscore")){
			return "體適能總分";
		}else if(name.equals("dtotalscore")){
			return "運動習慣總分";
		}else if(name.equals("ftotalscore")){
			return "飲食習慣總分";
		}else if(name.equals("BMI")){
			return "BMI";
		}else if(name.equals("hwscore")){
			return "身高體重比例";
		}else if(name.equals("total")){
			return "整體總分";
		}else if(name.equals("d1score")){
			return "D1";
		}else if(name.equals("d2score")){
			return "D2";
		}else if(name.equals("d3score")){
			return "D3";
		}else if(name.equals("d4score")){
			return "D4";
		}else if(name.equals("d5score")){
			return "D5";
		}else if(name.equals("d6score")){
			return "D6";
		}else if(name.equals("d7score")){
			return "D7";
		}else if(name.equals("d8score")){
			return "D8";
		}else if(name.equals("d9score")){
			return "D9";
		}else if(name.equals("d10score")){
			return "D10";
		}else if(name.equals("d11score")){
			return "D11";
		}else if(name.equals("d12score")){
			return "D12";
		}else if(name.equals("d13score")){
			return "D13";
		}else if(name.equals("d14score")){
			return "D14";
		}else if(name.equals("d15score")){
			return "D15";
		}else if(name.equals("f1score")){
			return "F1";
		}else if(name.equals("f2score")){
			return "F2";
		}else if(name.equals("f3score")){
			return "F3";
		}else if(name.equals("f4score")){
			return "F4";
		}else if(name.equals("f5score")){
			return "F5";
		}else if(name.equals("f6score")){
			return "F6";
		}else if(name.equals("f7score")){
			return "F7";
		}else if(name.equals("f8score")){
			return "F8";
		}else if(name.equals("f9score")){
			return "F9";
		}else if(name.equals("f10score")){
			return "F10";
		}
		
		return "";
	}
}
